package day325;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tmh
 * @date 2023/3/25 17:22
 * @description 括号生成
 */
public class T22 {
    private List<String> list = new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        valid(0, 0, n, "");
        return list;
    }

    public void valid(int left, int right, int n, String s) {
        //满足了条件之后就往list集合里面添加s
        if (left == n && right == n) {
            list.add(s);
        }
        //处理当前层的逻辑,如果左括号的个数小于n，那么就加左括号，之后再往下一层去
        //如果右括号的个数小于左括号的个数，那么就去加上右括号，之后再往下一层去
        if (left < n) {
            valid(left + 1, right, n, s + "(");
        }
        if (right < left) {
            valid(left, right + 1, n, s + ")");
        }
    }

}
